package com.glory.wxmemsicpda.utils;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import static android.view.View.FOCUS_DOWN;

/**
 * FileName: EditTextEnterUtils
 * Author: XJH
 * Date: 2020/5/12 16:24
 * Description: edit辅助类
 * History:
 */
public class EditTextEnterUtils {

    public static void initPdaEditText(EditText editText, boolean focus, OnEnterListener onEnterListener) {
        initPdaEditText(editText, focus, onEnterListener, null);
    }

    public static void initPdaEditText(final EditText editText, boolean focus, final OnEnterListener onEnterListener, OnEnterKeyListener onEnterKeyListener){
        if (focus) {
            editText.requestFocus();
        }
        initEnterToUpperCase(editText, onEnterKeyListener);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        onEnterListener.onPdaEnter(s.toString());
                    } else {
                        FreeApi_ToastUtils.showFreeToast("扫描信息有误", editText.getContext(), false, 1);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 点击Enter，文本小写转大写
     *
     * @param editText
     */

    public static void initEnterToUpperCase(EditText editText) {
        initEnterToUpperCase(editText, null);
    }

    public static void initEnterToUpperCase(final EditText editText, final OnEnterKeyListener onEnterKeyListener) {
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);
        editText.setSingleLine(true);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_GO == i) {
                    String text = editText.getText().toString();
                    if (onEnterKeyListener != null) {
                        onEnterKeyListener.onEnterKey(text);
                    }
                    //转大写并触发扫描效果
                    editText.getText().clear();
                    editText.setText(text.toUpperCase());
                    editText.setSelection(editText.getText().length());
//                nextEditTextFocus(editText);      跳转下一个
                    return true;
                }
                return false;
            }
        });
    }

    /** 使当前输入框的下一个输入框获取焦点
     * @param editText
     */
    public static void nextEditTextFocus(EditText editText) {
        View v = editText.focusSearch(View.FOCUS_DOWN);
        if (v != null) {
            if (!v.requestFocus(FOCUS_DOWN)) {
                throw new IllegalStateException("focus search returned a view "
                        + "that wasn't able to take focus!");
            }
        }
    }

    /** 设置并监听Enter键，并添加小写转大写的过滤器
     * @param editText
     * @param onEnterListener
     */
    @Deprecated
    public static void initEnter(EditText editText, final OnEnterListener onEnterListener) {
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);
        editText.setSingleLine(true);
        InputFilter[] inputFilters = editText.getFilters();
        if (inputFilters != null && inputFilters.length > 0) {
            InputFilter[] newInputFilters = new InputFilter[inputFilters.length + 1];
            for (int i = 0; i < inputFilters.length; i++) {
                newInputFilters[i] = inputFilters[i];
            }
            newInputFilters[inputFilters.length] = new ToUpperCase();
            editText.setFilters(newInputFilters);
        } else {
            editText.setFilters(new InputFilter[]{new ToUpperCase()});
        }

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (EditorInfo.IME_ACTION_GO == i) {
                    if (onEnterListener != null) {
                        onEnterListener.onPdaEnter(textView.getText().toString());
                    }
                    return true;
                }
                return false;
            }
        });
    }

    public interface OnEnterListener{
        void onPdaEnter(String text);
    }

    public interface OnEnterKeyListener{
        void onEnterKey(String text);
    }

    public static class ToUpperCase implements InputFilter {
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            return charSequence.toString().toUpperCase();
        }
    }
}
